<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form">
          <h1>{{ constData.welcomeTips }}</h1>
          <h2>{{ constData.projectName }}</h2>
          <el-form-item>
            <el-input
              placeholder="用户名"
              :prefix-icon="User"
              v-model="loginForm.username"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              placeholder="密码"
              type="password"
              :prefix-icon="Lock"
              show-password
              v-model="loginForm.password"
            />
          </el-form-item>
          <el-form-item>
            <el-button class="login_btn" type="primary" size="default" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue";
import { reactive } from "vue";
import {constData} from "@/util/consts.ts";
import useUserStore from "@/store/modules/user";
import { ElNotification } from 'element-plus';
import { useRouter, useRoute } from 'vue-router';
import {getTime} from "@/util/time.ts";

let userStore = useUserStore();
let $router = useRouter();
let $route = useRoute();
const loginForm = reactive({'username': 'admin', 'password': '1'});
const login = async () => {
  try {
    await userStore.userLogin(loginForm);
    let redirect: any = $route.query.redirect;
    await $router.push({path: redirect || '/'});
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `HI,${getTime()}好`
    });
  }
  catch(error) {
    ElNotification({
      type: 'error',
      message: (error as Error).message
    })
  }
}

</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg") no-repeat;
  background-size: cover;
  .login_form {
    position: relative;
    width: 80%;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;
    top: 30vh;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
